﻿
@{
    ViewBag.Title = "Tables_reports";
}



<from class="from-horizontal">
    <div class="row">
        <fieldset class="boun nav" style="padding-bottom:6px;">
            <legend class="len"></legend>
            <div class="col-lg-1">发货单号</div>
            <div class="col-lg-3">
                <select id="danhao">
                    
                </select>
            </div>
            <div class="col-lg-8" style="text-align:right">
                <button id="tberp">附件同步到ERP</button>
            </div>
        </fieldset>
    </div>
    <div class="tab">
        <table class="table table-sm gun" border="1">
            <thead class="thead-light">
                <tr style="text-align:center">
                    <th scope="col">单号</th>
                    <th scope="col">客户名称</th>
                    <th scope="col">项目名称</th>
                    <th scope="col">回单地址</th>
                    <th scope="col">操作</th>
                </tr>
            </thead>
            <tbody id="AppTable" style="height:500px;">
            </tbody>
        </table>
    </div>
</from>

@section scripts
{
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script>
        $(function () {
            Application_form_information()
            Drop_down_box()

            $(document).on("change", "#danhao", function () {
                Application_form_information()
            })
            $(document).on("click", "#tberp",function () {
                synchronization()
            })
            //附件同步到数据库
            function synchronization() {
                var fhdh = []
                $(".fhsj").each(function (i, item) {
                    var wjlj = $("td[class=wjlj]", item).text()
                    console.log(wjlj)
                    if (wjlj != "") {
                        fhdh.push({
                            hd_fhdh: $("td[class=fhdh_id]", item).text()
                        })
                    }
                    
                })
                console.log(fhdh)
                $.ajax({
                    url: "/Attachment_matching/add_tbjl",
                    type: 'POST',
                    data: JSON.stringify(fhdh),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (text) {
                        alert(text)
                    },
                    error: function (text) {
                        alert("系统错误" + text);
                    }
                })
            }
            //查找单号放在表格
            function Application_form_information() {
                var combs = $("#danhao").val()

                $.ajax({
                    url: '/Attachment_matching/Tables_reports',
                    type: 'POST',
                    data: { "state": 1, "combs": combs },
                    success: function (data) {
                        createRow(data);
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })

                function createRow(data) {
                    $('#AppTable tr').remove();
                    $.each(data, function (i, item) {
                        var temp=0
                        $(".fhsj").each(function (x, obj) {
                            if (obj.id == item.id) {
                                temp=1
                            }
                        })
                        if (temp != 1) {
                            var filepath = item.FilePath
                            var $tr = 
                                $(`
                                    <tr class="fhsj" id="${item.id}">
                                        <td class="fhdh_id">${item.id}</td>
                                        <td>${item.client}</td>
                                        <td>${item.item}</td>
                                        <td class="wjlj">${item.FilePath}</td>
                                        <td><a href="${filepath}">下载</a></td>
                                    </tr>

                                `);
                            $('#AppTable').append($tr);
                        }
                        
                    })
                }
                
            }
            //查找单号放在下拉列表
            function Drop_down_box() {
                $.ajax({
                    url: '/Attachment_matching/Tables_reports',
                    type: 'POST',
                    data: { "state": 2 },
                    success: function (data) {
                        $("#danhao option").remove()
                        $("#danhao").append($(`<option value="0" class="fhdh">---请选择---</option>`))
                        $.each(data, function (i, item) {
                            var index = 0
                            $(".fhdh").each(function (o, obj) {
                                if ($(obj).val() == item.id) {
                                    index = 1
                                }
                            })
                            if (index != 1) {
                                $("#danhao").append($(`<option value="${item.id}" class="fhdh">${item.id} ${item.item}</option>`))
                                
                            }
                        })
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }
        })
    </script>
}

